package com.ming.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ming.constant.StatusConstant;
import com.ming.dto.SetmealDTO;
import com.ming.dto.SetmealPageQueryDTO;
import com.ming.entity.Setmeal;
import com.ming.entity.SetmealDish;
import com.ming.exception.SetmealEnableFailedException;
import com.ming.mapper.SetmealDishMapper;
import com.ming.mapper.SetmealMapper;
import com.ming.result.PageResult;
import com.ming.service.SetmealService;
import com.ming.vo.SetmealVO;
import com.ming.vo.SetmealVOWithDishes;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐和菜品
     * @param setmealDTO
     */
    @Transactional
    public void saveWithDishes(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        // 套餐状态默认为启用状态
        setmeal.setStatus(StatusConstant.ENABLE);

        // 保存套餐基本信息
        setmealMapper.insert(setmeal);

        // 获取套餐ID
        Long setmealId = setmeal.getId();

        // 保存套餐菜品关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        List<SetmealVO> setmealVOList = setmealMapper.pageQuery(setmealPageQueryDTO);

        Page<SetmealVO> page = (Page<SetmealVO>) setmealVOList;

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据ID查询套餐和菜品
     * @param id
     * @return
     */
    public SetmealVOWithDishes getByIdWithDishes(Long id) {
        // 查询套餐基本信息
        SetmealVO setmealVO = setmealMapper.getById(id);

        // 查询套餐包含的菜品
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);

        // 构建返回对象
        SetmealVOWithDishes setmealVOWithDishes = new SetmealVOWithDishes();
        BeanUtils.copyProperties(setmealVO, setmealVOWithDishes);
        setmealVOWithDishes.setSetmealDishes(setmealDishes);

        return setmealVOWithDishes;
    }

    /**
     * 根据ID删除套餐
     * @param id
     */
    @Transactional
    public void deleteById(Long id) {
        // 查询套餐状态
        Setmeal setmeal = setmealMapper.getSetmealById(id);
        if (setmeal != null && setmeal.getStatus() == StatusConstant.ENABLE) {
            throw new SetmealEnableFailedException("启用的套餐不能删除");
        }

        // 删除套餐菜品关系
        setmealDishMapper.deleteBySetmealId(id);

        // 删除套餐
        setmealMapper.deleteById(id);
    }

    /**
     * 根据ID批量删除套餐
     * @param ids
     */
    @Transactional
    public void deleteByIds(List<Long> ids) {
        // 查询套餐状态，检查是否有启用的套餐
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getSetmealById(id);
            if (setmeal != null && setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new SetmealEnableFailedException("启用的套餐不能删除");
            }
        }

        // 删除套餐菜品关系
        setmealDishMapper.deleteBySetmealIds(ids);

        // 删除套餐
        setmealMapper.deleteByIds(ids);
    }

    /**
     * 修改套餐和菜品
     * @param setmealDTO
     */
    @Transactional
    public void updateWithDishes(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        // 更新套餐基本信息
        setmealMapper.update(setmeal);

        // 删除原有的套餐菜品关系
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());

        // 保存新的套餐菜品关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealDTO.getId());
            });
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 启用或禁用套餐
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, Long id) {
        if (status == StatusConstant.ENABLE) {
            // 启用套餐时，检查套餐内菜品状态
            List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
            // 这里应该检查菜品状态，但需要引入DishMapper，为简化暂不实现
        }

        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 根据分类ID查询套餐
     * @param categoryId
     * @return
     */
    public List<Setmeal> getByCategoryId(Long categoryId) {
        return setmealMapper.getByCategoryId(categoryId);
    }
}